package cc.yaomm.common.basic.file;

import cc.yaomm.common.basic.base.SuperEnum;
import cc.yaomm.common.basic.exception.BusinessException;
import cc.yaomm.common.basic.file.properties.FileProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 * Created by XQL on 2020/5/15.
 */
@Slf4j
public class LocalDiskService implements FileService {

    @Autowired
    FileProperties fileProperties;

    @Override
    public FileProperties getFileProperties() {
        return fileProperties;
    }

    @Override
    public String upload(String fileName, byte[] fileData, SuperEnum<?, String> directory, boolean isRewrite) {

        //判断基础路径是否为空
        if (StringUtils.isEmpty(fileProperties.getBasePath())) {
            throw new BusinessException("基础路径不能为空，请配置file.base-path参数");
        }

        //判断基础路径是否以/结尾
        String basePath = fileProperties.getBasePath();
        if (basePath.endsWith(File.separator)) {
            basePath = basePath.concat(directory.getValue());
        } else {
            basePath = basePath.concat(File.separator).concat(directory.getValue());
        }

        //获取文件的完成路径
        String filePath = this.getFilePath(basePath, fileName, isRewrite);

        //获取文件目录
        String fileDirectory = filePath.substring(0, filePath.lastIndexOf(File.separator));
        File fileDir = new File(fileDirectory);
        //判断目录是否存在，不存在自动创建
        if (!fileDir.exists()) {
            log.info("创建文件目录：{}", fileDirectory);
            fileDir.mkdirs();
        }

        try {
            //转存文件
            FileCopyUtils.copy(fileData, new File(filePath));
        } catch (IOException e) {
            log.error("保存失败,源文件名称：{}，文件路径：{}", fileName, filePath);
            throw new BusinessException("保存文件异常", e);
        }

        return filePath;
    }

    @Override
    public boolean delete(String filePath) {
        //判断基础路径是否为空
        if (StringUtils.isEmpty(fileProperties.getBasePath())) {
            throw new BusinessException("基础路径不能为空，请配置file.base-path参数");
        }

        //构建完整路径
        filePath = fileProperties.getBasePath().concat(File.separator).concat(filePath);

        //删除文件
        try {
            return Files.deleteIfExists(Paths.get(filePath));
        } catch (IOException e) {
            log.error("删除文件异常-文件路径：{}", filePath, e);
            return false;
            //e.printStackTrace();
        }
    }

    @Override
    public boolean delete(List<String> filePathList) {
        //判断参数是否为空
        if (CollectionUtils.isEmpty(filePathList)) {
            return false;
        }

        //判断基础路径是否为空
        if (StringUtils.isEmpty(fileProperties.getBasePath())) {
            throw new BusinessException("基础路径不能为空，请配置file.base-path参数");
        }

        boolean result = false;
        for (String filePath : filePathList) {
            //构建完整路径
            filePath = fileProperties.getBasePath().concat(File.separator).concat(filePath);
            try {
                result = Files.deleteIfExists(Paths.get(filePath));
            } catch (IOException e) {
                //e.printStackTrace();
                log.error("批量删除文件异常-文件路径：{}", filePath, e);
            }
        }

        return result;
    }

}
